import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

public class ZipWriter {
    protected FileOutputStream fos;
    protected ZipOutputStream zos;

    public ZipWriter(String name) {
        try {
            fos = new FileOutputStream(name);
            zos = new ZipOutputStream(fos);
        } catch (IOException e) {
            System.err.println(e.getLocalizedMessage());
        }
    }

    public void addEntry(String entryName, String content) {
        byte[] bytes = content.getBytes(StandardCharsets.UTF_8);
        addEntry(entryName, bytes);
    }

    public void addEntry(String entryName, byte[] bytes) {
        ZipEntry entry = new ZipEntry(entryName);
        try {
            zos.putNextEntry(entry);
            zos.write(bytes, 0, bytes.length);
            zos.closeEntry();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    public void close() {
        try {
            zos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

        try {
            fos.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
